HITL-PX4 Tutorial
Hardware-in-the-loop simulation
Hardware-in-the-loop (HITL or HIL) is a simulation mode where regular PX4 firmware runs on real flight controller hardware. The benefit of this approach is that most of the actual flight code can be tested on real hardware.
PX4 supports both HITL (using jMAVSim or Gazebo Classic ) and VTOL (using Gazebo Classic) for multirotors.
HITL compatible
Compatible devices and simulator settings are:
| Body | SYS_AUTOSTART | Pavilion Classic | Simulation MAVSim |
|---|---|---|---|
| HIL QuadcopterX | 1001 | yes | yes |
| HIL standard VTOL quadcopter | 1002 | yes |
HITL simulation
Through hardware-in-the-loop (HITL) simulation, regular PX4 firmware can be run on real hardware. JMAVSim or Gazebo Classic (running on the development computer) connects to the flight controller hardware via USB/UART. The simulator acts as a gateway, sharing MAVLink data between PX4 and QGroundControl.
information
It is also possible to connect to the simulator via UDP if the flight controller supports networking and uses a stable, low-latency connection (e.g. a wired Ethernet connection - WiFi is often unreliable). For example, this configuration has been tested running PX4 on a Raspberry Pi connected to a computer via Ethernet (the launch configuration containing the command to run jMAVSim can be found in px4_hil.config ).
The following image shows the simulation environment:
- A HITL configuration was chosen (via QGroundControl) that does not enable any real sensors .
- jMAVSim or Gazebo Classic is connected to the flight controller via USB.
- The simulator connects to QGroundControl via UDP and bridges its MAVLink messages to PX4.
- Gazebo Classic and jMAVSim can also connect to off-board APIs and bridge MAVLink messages to PX4.
- Optionally, joystick/gamepad hardware can be connected via QGroundControl using a serial connection.
HITL and
SITL runs on a development computer in a simulated environment and uses firmware built specifically for that environment. The system operates normally except that the simulation driver provides fake environment data from the simulator.
In contrast, HITL runs regular PX4 firmware in "HITL mode" on regular hardware. Simulation data enters the system at a different point than in SITL. Core modules, such as commanders and sensors, enter HITL mode at boot time, bypassing some normal functionality.
In summary, HITL uses standard firmware to run PX4 on real hardware, but SITL actually executes more standard system code.
Setting up
Check if there is
The modules required for HITL ( pwm_out_sim) are not built into all PX4 firmware by default.
To check if the module is present on your flight controller:
- Open QGroundControl
- Open Analysis Tools > Mavlink Console.
- Enter the following command in the console:
嘘pwm_out_sim status - If the return value is yes
nsh: pwm_out_sim: command not found, it means you don't have the module installed.
If pwm_out_sim it is not present, you will need to add it to your firmware in order to use HITL simulation.
Adding HITL modules to
Add the following key to your flight controller's configuration file to include the required modules (for example, see board/px4/fmu-v6x/default.px4board ). Then rebuild and flash the firmware to the board.
文本CONFIG_MODULES_SIMULATION_PWM_OUT_SIM=y
You can also launch the GUI configuration tool using the following command and enable them interactively at: modules > Simulation > pwm_out_sim. For example, to update fmu-v6x, you can use:
PX4
- Connect the autopilot directly to QGroundControl via USB.
- Select the body
- Open "Settings" > "Device"
- Select the compatible device you want to test . Then click "Apply and Restart" in the upper right corner of the device settings page.
- If necessary, calibrate your RC or joystick.
- Setting up UDP
- On the General tab of the Settings menu , uncheck all the Auto-connect boxes except UDP.

- On the General tab of the Settings menu , uncheck all the Auto-connect boxes except UDP.
- (Optional) Configure the joystick and failsafe. Set the following parameters to use a joystick instead of an RC remote:
- COM_RC_IN_MODE is set to “Joystick/No RC Check.” This allows joystick input and disables RC input checking.
- NAV_RCL_ACT is set to "Disabled". This ensures that when running HITL without radio control, there will be no interference from any RC failsafe operation. Tip The QGroundControl User Guide also contains instructions for joystick and virtual joystick setup.
Once configuration is complete, close QGroundControl and disconnect the flight controller hardware from your computer.
Simulator-specific
Follow the appropriate setup steps for your specific emulator in the following sections.
Pavilion
information
Make sure QGroundControl is not running!
- Build PX4 using Gazebo Classic (for building Gazebo Classic plugins).
- Open the sdf file of the vehicle model (for example, Tools/simulation/gazebo-classic/sitl_gazebo-classic/models/iris_hitl/iris_hitl.sdf ).
- Replace
serialDevicethe _parameter ( _) if necessary./dev/ttyACM0The serial device you see depends on the port you used to connect the vehicle to your computer (usually/dev/ttyACM0_). An easy way to check this on Ubuntu is to plug in the autopilot, open a terminal, and type _dmesg | grep "tty". The correct device will be the last one displayed. - Set the environment variables: and run Gazebo Classic in HITL mode:
嘘gazebo Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/hitl_iris.world - Start QGroundControl. It should automatically connect to PX4 and Gazebo Classic.
jMAVSim (quadrotors only
information
Make sure QGroundControl is not running!
- Connect the flight controller to your computer and wait for it to boot up.
- Running jMAVSim in HITL mode: Information
/dev/ttyACM0Replace the serial port name as appropriate. On macOS, this is/dev/tty.usbmodem1. On Windows (including Cygwin), this is COM1 or another port - check the connection in Windows Device Manager. - Start QGroundControl. It should automatically connect to PX4 and jMAVSim.
Executing autonomous
You should be able to use QGroundControl to run missions and control the vehicle in other ways.
More information
Reference link: https://docs.px4.io/main/en/simulation/hitl.html